//
//  ViewController.m
//  21作业XML解析
//
//  Created by dllo on 16/8/10.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "XMLReader.h"
#import "Model.h"
#import "GDataXMLNode.h"

@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic,copy) NSString *currentElementName;
@property (nonatomic,strong) NSMutableArray *modelArr;

@property (nonatomic,strong) NSMutableArray *domModelArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"Cartoon" ofType:@"xml"];
    //    NSLog(@"%@",xmlPath);
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
    NSXMLParser *xmlParse = [[NSXMLParser alloc] initWithData:xmlData];
    NSError *error = nil;
    NSDictionary *xmlDic = [XMLReader dictionaryForXMLData:xmlData error:&error];
//    NSLog(@"%@",xmlDic);

}

- (IBAction)saxParse:(id)sender {
//    NSLog(@"小地方");
    NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"Cartoon" ofType:@"xml"];
//    NSLog(@"%@",xmlPath);
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
    NSXMLParser *xmlParse = [[NSXMLParser alloc] initWithData:xmlData];
    xmlParse.delegate = self;
    [xmlParse parse];
}
//解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    _modelArr = [NSMutableArray array];
}
//节点头
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
    _currentElementName = elementName;
//    NSLog(@"%@",elementName);
    if ([elementName isEqualToString:@"AlbumInfo"]) {
        Model *model = [Model new];
        [_modelArr addObject:model];
    }
//    NSLog(@"%@",_modelArr);
    
    
}
//节点的值
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (_currentElementName) {
        Model *model = [_modelArr lastObject];
        [model setValue:string forKey:_currentElementName];
    }
}
//节点的尾
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    _currentElementName = nil;
}
//解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"%@",_modelArr);
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSLog(@"%@",parseError);
}



//DOM解析器
- (IBAction)domParse:(id)sender {
    _domModelArr = [NSMutableArray array];
//    NSLog(@"点击返回");
    NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"Cartoon" ofType:@"xml"];
//    NSLog(@"%@",xmlPath);
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
    NSError *error = nil;
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
//    NSLog(@"%@",document);
    GDataXMLElement *rootElement = [document rootElement];
//    NSLog(@"%@",rootElement.name);
    NSArray *rootChildren = [rootElement children];
//    NSLog(@"%ld",rootChildren.count);
    for (GDataXMLElement *childrenElement in [[rootChildren firstObject] children]) {
        Model *model = [Model new];
        [_domModelArr addObject:model];
        for (GDataXMLElement *grandSonElement in [childrenElement children]) {
            [model setValue:grandSonElement forKey:grandSonElement.name];
            
        }
    }
    NSLog(@"%@",_domModelArr);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
